// components/navigationBar/normal/normal.js
const app = getApp();
Component({
  properties: {
    navbarData: { //navbarData   由父页面传递的数据，变量名字自命名
      type: Object,
      value: {},
      observer: function (newVal, oldVal) { }
    },
    nowUserId: app.globalData.userId,
    userList: app.globalData.userList,
    nowQueueId: app.globalData.queueId
  },

  data: {
    height: '',
    showModal: false,
    //默认值  默认显示左上角
    navbarData: {
      showCapsule: 1
    },
    disabled: true
  },
  attached: function () {
    // 获取是否是通过分享进入的小程序
    // this.setData({
    //   share: app.globalData.share
    // })
    // 定义导航栏的高度   方便对齐
    this.setData({
      // height: app.globalData.height
      statusBarHeight: app.globalData.statusBarHeight,
    })
  },
  methods: {
    //切换用户
    showModalFn() {
      this.setData({
        showModal: true
      })
    },
    hideModalFn() {
      this.setData({
        showModal: false
      });
    },
    // 对用户列表进行排序，将当前用户放在第一位
    userSort(userList) {
      console.log('current user id: ' + this.properties.nowUserId)
      console.log('current queue id: ' + this.properties.nowQueueId)

      if (userList) {
        var index = null
        for (var i = 0; i < userList.length; i++) {
          if (this.properties.nowUserId == userList[i].id && this.properties.nowQueueId == userList[i].queueId) {
            index = i;
            break;
          }
        }
        if (index&&index != 0) {
          var userListIndex = userList[0]
          userList[0] = userList[index]
          userList[index] = userListIndex
        }
      }
      console.log(userList)
      return userList
    },
    changeToUser(obj) {
      var that = this;
      var data = {
        nowUserId: '',
        nowQueueId: ''
      }
      if (obj.id) {
        data.nowUserId = obj.id
        data.nowQueueId = obj.queueId
      } else {
        data.nowUserId = obj.currentTarget.dataset.userid
        data.nowQueueId = obj.currentTarget.dataset.queueid
      }

      //下面为通过用户id和队列id获取用户的请求
      wx.request({
        url: app.getUrl('/xcx/pt/checkUserInfo'),
        header: wx.getStorageSync('cookie'),
        data: {
          userId: data.nowUserId,
          queueId: data.nowQueueId
        },
        success(res) {
          if (res.data.code == 200) {
            if (!obj.id) {
              wx.showLoading({
                title: "正切换至" + obj.currentTarget.dataset.username + "(" + obj.currentTarget.dataset.queuename + ")",
              })
            }
            setTimeout(function () {
              wx.hideLoading()
              //刷新小程序
              that.hideModalFn();
              var pages = getCurrentPages();
              app.globalData.userId = data.nowUserId;
              app.globalData.queueId = data.nowQueueId;
              that.setData({
                nowUserId: data.nowUserId,
                nowQueueId: data.nowQueueId,
              })
              if (pages.length != 0) {
                //刷新当前页面的数据
                for (var i = 0; i < pages.length; i++) {
                  pages[i].onLoad();
                  pages[i].onShow();
                }
              }
              that.setData({
                userList: that.userSort(app.globalData.userList)
              })
            }, 500)
          }else if(res.data.code == 0){
            wx.showToast({
              title: '该用户未通过审核或已被删除,切换中',
              icon:'none',
              duration:2000
            })
            setTimeout(function (){
              that.hideModalFn();
              var pages = getCurrentPages();
              if(res.data.data!=null){
                app.globalData.userList = res.data.data
                app.globalData.userId = res.data.data[0].id;
                app.globalData.queueId = res.data.data[0].queueId;
              }else{
                app.globalData.userList = null
                app.globalData.userId = null
                app.globalData.queueId = null
              }
              if (pages.length != 0) {
                //刷新当前页面的数据
                for (var i = 0; i < pages.length; i++) {
                  pages[i].onLoad();
                  pages[i].onShow();
                }
              }
              that.setData({
                userList: that.userSort(app.globalData.userList)
              })
              if (app.globalData.userId && app.globalData.queueId) {
                that.setData({
                  disabled: false,
                  nowUserId: app.globalData.userId,
                  nowQueueId: app.globalData.queueId
                })
              }
              if(app.globalData.userList==null){
                that.setData({
                  disabled:true
                })
              }
            },1000)
          }
        }
      })

      //下面为通过用户id获取用户队列的请求，页面会跳转到队列选择界面
      /*wx.request({
        url: app.getUrl('/xcx/pt/checkUserQueue'),
        header: wx.getStorageSync('cookie'),
        data: {
          userId: e.currentTarget.dataset.userid
        },
        success(res) {
          wx.showLoading({
            title: "正切换至" + e.currentTarget.dataset.username,
          })
          setTimeout(function() {
            wx.hideLoading()
            if (res.data.code == 0) {
              //跳转至选择队列
              that.hideModal();
              wx.navigateTo({
                url: "../guide/queue_list/queueList?ptUserId=" + e.currentTarget.dataset.userid,
              })
            } else if (res.data.code == 200) {
              //刷新小程序
              that.hideModal();
              var pages = getCurrentPages();
              // that.setData({
              //   nowUserId: e.currentTarget.dataset.userid
              // })
              app.globalData.userId = e.currentTarget.dataset.userid;
              if (pages.length != 0) {
                //刷新当前页面的数据
                for (var i = 0; i < pages.length; i++) {
                  pages[i].onLoad();
                  pages[i].onShow();
                }
              }
            }
            that.setData({
              userList: that.userSort(app.globalData.userList)
            })
          }, 1000);
        }
      })*/


      // wx.request({
      //   url: app.getUrl('/xcx/changeDefaultUser'),
      //   header: wx.getStorageSync('cookie'),
      //   data :{
      //     userId:e.currentTarget.dataset.userid
      //   },
      //   success(res) {
      //     if (res.data.code == 0) {
      //       //刷新小程序
      //       that.hideModal();
      //     }
      //   }
      // })
    },
    //用户添加操作
    toAddNewUser() {
      this.hideModalFn();
      wx.navigateTo({
        url: '../guide/queue/queue?code=true',
      })
    },
    toExitPro: function(){
      //点击退出时去设置微信用户状态
      wx.request({
        url: app.getUrl('/xcx/exit'),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        success(res) {
          wx.navigateBack()
        }
      })
    },
    setUserList: function () {
      this.setData({
        userList: this.userSort(app.globalData.userList)
      })
    },
    // 获取用户列表
    getUserList: function () {
      console.log(1)
      let that = this
      wx.request({
        url: app.getUrl('xcx/pt/changeUsersInfo'),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        success(res) {
          if (res.data.code == 401) {
            setTimeout(that.getUserList, 1000)
            return
          }
          if (res.data.code != 0) {
            app.globalData.userList = null
            that.setData({
              userList: null
            })
            return
          }
          app.globalData.userList = res.data.data
          that.judgeCurrentPatient()
          // that.setUserList()
        }
      })
      if (app.globalData.userMobile) {
        that.setData({
          disabled: false,
          nowUserId: app.globalData.userId,
          nowQueueId: app.globalData.queueId
        })
      }
    },
    judgeCurrentPatient: function () {
      console.log(app.globalData.currentPatient)
      if (typeof app.globalData.currentPatient == 'object') {
        const { currentPatient, userList, userId } = app.globalData
        userList.forEach(item => {
          if ((item.id === userId) && (item.queueName !== currentPatient.queueName)) {
            this.changeToUser(item)
          }
        })
        return
      }
      console.log(1)
      setTimeout(() => this.judgeCurrentPatient(), 500)
    }
  },
  lifetimes: {
    // 组件所在页面的生命周期函数
    ready() {
      this.getUserList()
    }
  },
})